import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/index")
public class indexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据当前的用户请求中的sessionId，获取到用户信息，并显示到页面上
        resp.setContentType("text/html;charset=utf-8");
        //1.判定当前的用户是否已经登陆了
        //为false是没有的话也不创建
        HttpSession httpSession=req.getSession(false);
        if(httpSession==null){
            resp.sendRedirect("login.html");
            return;
        }
        //2.用户已经登陆了，就可以从HttpSession中拿到用户信息
        String username=(String) httpSession.getAttribute("username");
        Integer logincount=(Integer) httpSession.getAttribute("logincount");
        logincount=logincount+1;
        httpSession.setAttribute("logincount",logincount);
        //3.返回一个html页面
        StringBuffer html=new StringBuffer();
        html.append("<div>username:"+username+"</div>");
        html.append("<div>logincount:"+logincount+"</div>");
        resp.getWriter().write(html.toString());


    }
}
